#include "model/model_file.h"
#include "headers.h"
#include <mysql/mysql.h>
//
int selectFileByid(MYSQL *conn, int id, FILE_M *file) {
	if(conn == NULL || file == NULL || id <= 0)
		return -1;
	char sql[1024] = {0};
	MYSQL_ROW row;
	sprintf(sql, "select * from file_7th where id = %d;", id);
	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}

	MYSQL_RES *result = mysql_store_result(conn);
	if(result == NULL) {
		printf(" mysql_store_result error:%s\n", mysql_error(conn));
		return -1;
	}

	if((row = mysql_fetch_row(result)) == NULL) {
		printf("mysql_fetch_row null data\n");
		return 0;
	}

	// unsigned long *lengths;
	// lengths = mysql_fetch_lengths(result);
	// for(i = 0; i < num_fields; i++) {
	// 	printf("[%.*s] ", (int)lengths[i], row[i] ? row[i] : "NULL");
	// }
	file->id = atoi(row[0]);
	strcpy(file->sha256_name, row[1]);
	file->file_size = atoi(row[2]);
	file->ref_count = atoi(row[3]);
	file->is_deleted = atoi(row[4]);

	mysql_free_result(result);
	return 0;
}

//
int selectFileBysha(MYSQL *conn, char sha[], FILE_M *file) {
	if(conn == NULL || file == NULL || sha == NULL)
		return -1;
	char sql[1024] = {0};
	MYSQL_ROW row;
	sprintf(sql, "select * from file_7th where sha256_name = '%s';", sha);

	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}

	MYSQL_RES *result = mysql_store_result(conn);
	if(result == NULL) {
		printf(" mysql_store_result error:%s\n", mysql_error(conn));
		return -1;
	}

	if((row = mysql_fetch_row(result)) == NULL) {
		printf("mysql_fetch_row null data\n");
		return 0;
	}

	// unsigned long *lengths;
	// lengths = mysql_fetch_lengths(result);
	// for(i = 0; i < num_fields; i++) {
	// 	printf("[%.*s] ", (int)lengths[i], row[i] ? row[i] : "NULL");
	// }
	file->id = atoi(row[0]);
	strcpy(file->sha256_name, row[1]);
	file->file_size = atoi(row[2]);
	file->ref_count = atoi(row[3]);
	file->is_deleted = atoi(row[4]);

	mysql_free_result(result);
	return 0;
}

// 返回-1表示参数不正确,1是系统失败，不符合表约束、初始失败等
int insertFile(MYSQL *conn, const FILE_M *file,int *file_id) {
	char sql[1024] = {0};
	sprintf(sql, "insert into file_7th(sha256_name,file_size,ref_count) value('%s',%d,%d);", file->sha256_name, file->file_size,1);
	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}

	if(mysql_affected_rows(conn)) {
		*file_id=mysql_insert_id(conn);
		return 0;
	}
	return 1;
}

// 递减引用,返回0表示执行成功，返回1表示减到0,并删除
int deleteFile(MYSQL *conn, const FILE_M *file) {
	if(conn == NULL || file == NULL || *file->sha256_name == '\0')
		return -1;
	FILE_M ft = {0};
	if(selectFileBysha(conn, (char *)file->sha256_name, &ft) || ft.id <= 0)
		return -1;
	char sql[1024] = {0};
	if(ft.ref_count >= 1) {
		sprintf(sql, "update file_7th set ref_count = %d where id = %d;", --ft.ref_count, ft.id);
		if(mysql_query(conn, sql)) {
			printf("mysql_query error:%s\n", mysql_error(conn));
			return -1;
		}
	}
	bzero(sql, 1024);
	if(ft.ref_count <= 0) {
		sprintf(sql, "update file_7th set is_deleted = %d where id = %d;", 1, ft.id);
		if(mysql_query(conn, sql)) {
			printf("mysql_query error:%s\n", mysql_error(conn));
			return -1;
		}
		return 1;
	}
	return 0;
}
// 增加计数
int updateFileRef(MYSQL *conn, const FILE_M *file) {
	if(conn == NULL || file == NULL || *file->sha256_name == '\0')
		return -1;
	FILE_M ft = {0};
	if(selectFileBysha(conn, (char *)file->sha256_name, &ft) || ft.id <= 0)
		return -1;
	char sql[1024] = {0};
	sprintf(sql, "update file_7th set ref_count = %d where id = %d;", ++ft.ref_count, ft.id);
	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}
	bzero(sql, 1024);
	if(ft.is_deleted == 1) {
		sprintf(sql, "update file_7th set is_deleted = %d where id = %d;", 0, ft.id);
		if(mysql_query(conn, sql)) {
			printf("mysql_query error:%s\n", mysql_error(conn));
			return -1;
		}
	}
	return 0;
}